﻿using System;
using System.Web.UI.WebControls;
using DAL.Repositories;

namespace GameAdmin.Pages
{
    public partial class answers : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            gw.RowDeleting += new GridViewDeleteEventHandler(gw_RowDeleting);
            gw.RowEditing += new GridViewEditEventHandler(gw_RowEditing);
        }

        void gw_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Response.Redirect("~/answer/" + gw.Rows[e.NewEditIndex].Cells[0].Text, true);
        }

        void gw_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int idToDelete = Int32.Parse(gw.Rows[e.RowIndex].Cells[0].Text);

            AnswerRepository.Delete(idToDelete);
            GetAnswers();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (RouteData.Values["questionId"] == null) return;

            if (!IsPostBack)
            {
                GetAnswers();
            }
        }

        private void GetAnswers()
        {
            string questionIdString = (string)RouteData.Values["questionId"];
            int questionId = Int32.Parse(questionIdString);

            literalId.Text = questionIdString;
            lnkNewAnswer.NavigateUrl += questionIdString;

            gw.DataSource = AnswerRepository.GetAnswers(questionId);
            gw.DataBind();
        }
    }
}